# Copyright 2017 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------------------------

CMAKE_MINIMUM_REQUIRED(VERSION 3.2 FATAL_ERROR)
PROJECT(sawtooth-poet CXX)

LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

SET (EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
SET (LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)

SET(DEPS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/deps")

# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# COMPILER OPTIONS
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# ADDITIONAL LIBRARIES
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

# SGX -- should move to findsgx 
OPTION(SGX_USE_SIMULATOR "Use the sgx simulator" FALSE)

IF(MSVC)
    SET(SGX_EDGER "$ENV{SGXSDKInstallPath}bin/win32/Release/sgx_edger8r.exe")
    SET(SGX_SIGN "$ENV{SGXSDKInstallPath}bin/win32/release/sgx_sign.exe")
    SET(SGX_LIB_DIRS_DEBUG "$ENV{SGXSDKInstallPath}bin/x64/Debug")
    SET(SGX_LIB_DIRS_RELEASE "$ENV{SGXSDKInstallPath}bin/x64/Release")
ELSE()
    SET(SGX_EDGER "$ENV{SGXSDKInstallPath}bin/x64/sgx_edger8r")
    SET(SGX_SIGN "$ENV{SGXSDKInstallPath}bin/x64/sgx_sign")
ENDIF()

SET(SGX_INCLUDE "$ENV{SGXSDKInstallPath}include")
SET(SGX_ENCLAVE_INCLUDE "$ENV{SGXSDKInstallPath}include"
                        "$ENV{SGXSDKInstallPath}include/tlibc"
                        "$ENV{SGXSDKInstallPath}include/stlport")

SET(SGX_LIBS_TRUSTED_NAMES sgx_tstdc sgx_tstdcxx sgx_tkey_exchange sgx_tcrypto)
SET(SGX_LIBS_UNTRUSTED_NAMES sgx_ukey_exchange)

IF(MSVC)
    SET(SGX_LIBS_UNTRUSTED_NAMES sgx_ukey_exchangemt ${SGX_LIBS_UNTRUSTED_NAMES})
ELSE()
    SET(SGX_LIBS_UNTRUSTED_NAMES ${SGX_LIBS_UNTRUSTED_NAMES} pthread )
ENDIF()

IF(${SGX_USE_SIMULATOR})
    ADD_DEFINITIONS(-D SGX_SIMULATOR)
    SET(SGX_LIBS_TRUSTED_NAMES sgx_trts_sim sgx_tservice_sim ${SGX_LIBS_TRUSTED_NAMES})
    SET(SGX_LIBS_UNTRUSTED_NAMES sgx_urts_sim sgx_uae_service_sim ${SGX_LIBS_UNTRUSTED_NAMES})
ELSE()
    SET(SGX_LIBS_TRUSTED_NAMES sgx_trts sgx_tservice ${SGX_LIBS_TRUSTED_NAMES})
    SET(SGX_LIBS_UNTRUSTED_NAMES sgx_urts sgx_uae_service ${SGX_LIBS_UNTRUSTED_NAMES})
ENDIF()

FOREACH(lib ${SGX_LIBS_TRUSTED_NAMES} ${SGX_LIBS_UNTRUSTED_NAMES})
    IF(MSVC)
	    SET(${lib} optimized ${SGX_LIB_DIRS_RELEASE}/${lib}${CMAKE_STATIC_LIBRARY_SUFFIX} debug ${SGX_LIB_DIRS_DEBUG}/${lib}${CMAKE_STATIC_LIBRARY_SUFFIX})
    ELSE()
        SET(${lib} ${lib})
    ENDIF()
ENDFOREACH(lib)

FOREACH(lib ${SGX_LIBS_TRUSTED_NAMES})
	SET(SGX_LIBS_TRUSTED ${SGX_LIBS_TRUSTED} ${${lib}})
ENDFOREACH(lib)

FOREACH(lib ${SGX_LIBS_UNTRUSTED_NAMES})
	SET(SGX_LIBS_UNTRUSTED ${SGX_LIBS_UNTRUSTED} ${${lib}})
ENDFOREACH(lib)

# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
FUNCTION(SGX_EDGE_TRUSTED EDL EDGE_FILES)
	GET_FILENAME_COMPONENT(EDL_BASE_NAME ${EDL} NAME_WE)
	INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
	
	SET (EDGE_FILES_LIST "${CMAKE_CURRENT_BINARY_DIR}/${EDL_BASE_NAME}_t.h" "${CMAKE_CURRENT_BINARY_DIR}/${EDL_BASE_NAME}_t.c") 
	SET (${EDGE_FILES} ${EDGE_FILES_LIST} PARENT_SCOPE) 
	ADD_CUSTOM_COMMAND( OUTPUT ${EDGE_FILES_LIST}
		COMMAND "${SGX_EDGER}" --trusted ${EDL} --search-path ${SGX_INCLUDE}
		WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
		DEPENDS ${PROJECT_EDL} 
	)	
ENDFUNCTION()

FUNCTION(SGX_EDGE_UNTRUSTED EDL EDGE_FILES)
	GET_FILENAME_COMPONENT(EDL_BASE_NAME ${EDL} NAME_WE)
	INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
	
	SET (EDGE_FILES_LIST "${CMAKE_CURRENT_BINARY_DIR}/${EDL_BASE_NAME}_u.h" "${CMAKE_CURRENT_BINARY_DIR}/${EDL_BASE_NAME}_u.c") 
	SET (${EDGE_FILES} ${EDGE_FILES_LIST} PARENT_SCOPE) 
	ADD_CUSTOM_COMMAND( OUTPUT ${EDGE_FILES_LIST}
		COMMAND "${SGX_EDGER}" --untrusted ${EDL} --search-path ${SGX_INCLUDE}
		WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
		DEPENDS ${PROJECT_EDL} 
	)	
ENDFUNCTION()

FUNCTION(SGX_SIGN_ENCLAVE TARGET KEY_FILE CONFIG)
	SET (ENCLAVE $<TARGET_FILE:${TARGET}>)

	SET (SIGNED_ENCLAVE ${LIBRARY_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/${TARGET}.signed${CMAKE_SHARED_LIBRARY_SUFFIX})
	SET (SIGNED_ENCLAVE ${SIGNED_ENCLAVE} PARENT_SCOPE)
	ADD_CUSTOM_COMMAND( TARGET ${TARGET}
		POST_BUILD
		COMMAND "${SGX_SIGN}" sign -key "${KEY_FILE}" -enclave "${ENCLAVE}" -out "${SIGNED_ENCLAVE}" -config "${CONFIG}"
	)	
ENDFUNCTION()

# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
IF( WIN32 )
	MACRO( SET_RUNTIME type )
		if(${type} STREQUAL "DYNAMIC")
			SET(RTS "/MT")
			SET(RTD "/MD")
		ELSEIF(${type} STREQUAL "STATIC")
			SET(RTS "/MD")
			SET(RTD "/MT")
		ELSE()
			MESSAGE(SEND_ERROR  "Unknown runtime ${type}")
		ENDIF()
		FOREACH(bt ${CMAKE_CONFIGURATION_TYPES})
			STRING(TOUPPER "CMAKE_C_FLAGS_${bt}" variable)
			STRING(REGEX REPLACE ${RTS} ${RTD} ${variable} "${${variable}}")
			STRING(TOUPPER "CMAKE_CXX_FLAGS_${bt}" variable)
			STRING(REGEX REPLACE ${RTS} ${RTD} ${variable} "${${variable}}")		
		ENDFOREACH()		
	ENDMACRO()	
ELSE()
	MACRO( SET_RUNTIME type )
	ENDMACRO()	
ENDIF()

# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

INCLUDE_DIRECTORIES(${SGX_INCLUDE})
LINK_DIRECTORIES()

# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# PROJECTS
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ADD_SUBDIRECTORY (sawtooth_poet_sgx/libpoet_enclave)
ADD_SUBDIRECTORY (sawtooth_poet_sgx/libpoet_bridge)
